using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS;
using Deepcode.Flux.Core.Services.Security;

public partial class Private_CMS_taskpads_checkins : System.Web.UI.UserControl
{
	/// <summary>
	/// Page_Load Event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			BindData();
		}
	}
	/// <summary>
	/// Page_Init Event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Init(object sender, EventArgs e)
	{
		datagrid.ItemCommand += new DataGridCommandEventHandler(datagrid_ItemCommand);
	}
	/// <summary>
	/// Bind the list of checkouts for the current user to the datagrid
	/// </summary>
	protected void BindData()
	{
		AdministrationSecuritySession u = AdministrationSecurity.GetSession();
		if( u == null ) return;

		ContentManager cms = new ContentManager(true);
		datagrid.DataSource = cms.AdxGetWaitingSubmissionFor( u.PK_ID );
		datagrid.DataBind();
	}
	/// <summary>
	/// Trap linkbutton commands issued against the grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void datagrid_ItemCommand(object source, DataGridCommandEventArgs e)
	{
		if (e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator) return;

		// Get the key of the row being actioned 
		int rowKey = (int)datagrid.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if (e.CommandName == "Submit")
		{
			Response.Redirect(String.Format(
				"~/admin/private/cms/main/actions/submit/chooseDefTaskpad.aspx?ContentID={0}",
				rowKey), true);
		}
	}
	/// <summary>
	/// Submit the content item selected
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSubmitSelected_Click(object sender, EventArgs e)
	{
		// Prepare a list of ID's for submission
		List<int> items = new List<int>();
		foreach (DataGridItem i in datagrid.Items)
		{
			CheckBox _chk = (CheckBox) i.FindControl("_chk");
			if( _chk == null ) continue;

			if( _chk.Checked )
				items.Add( (int) datagrid.DataKeys[i.ItemIndex] );
		}

		// Only proceed if we have any items
		if (items.Count > 0)
		{
			// Create a CSV list of items
			string IDs = "";
			foreach (int i in items)
			{
				if( IDs != "" ) IDs += ",";
				IDs += i.ToString();
			}

			Response.Redirect(String.Format(
				"~/admin/private/cms/main/actions/submit/chooseDefTaskpad.aspx?MultiIDs={0}",
				IDs), true);
		}
	}
	/// <summary>
	/// Selects all items for checkin
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSelectAll_Click(object sender, EventArgs e)
	{
		foreach (DataGridItem i in datagrid.Items)
		{
			CheckBox _chk = (CheckBox)i.FindControl("_chk");
			if (_chk == null) continue;

			_chk.Checked = true;
		}
	}
	/// <summary>
	/// Deselects all checkin items
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnDeselectAll_Click(object sender, EventArgs e)
	{
		foreach (DataGridItem i in datagrid.Items)
		{
			CheckBox _chk = (CheckBox)i.FindControl("_chk");
			if (_chk == null) continue;

			_chk.Checked = false;
		}
	}

}
